import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:wechat_kit/wechat_kit.dart';
import 'package:tracker/pages/home/vip_service/view/vip_service_page.dart';

import '../../../../common/log/x_logger.dart';
import '../../../../common/pay/ali_pay_utils.dart';
import '../../../../common/pay/wxpay_utils.dart';
import '../../../../provider/global_provider.dart';
import '../../../../utils/toast_utils.dart';

mixin VipServicePageMixin<T extends ConsumerStatefulWidget>
    on ConsumerState<T> {
  List<String> tabs = ["安防录音", "高清录音", "录音恢复","轨迹服务"];

  late TabController tabController;

  final alipayUtils = AliPayUtils();
  final wxpayUtils = WxpayUtils();

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      wxpayUtils.registerWechatResp((resp) {
        XLogger.info('wxpayUtils exMsg: ${resp}');

        if (resp.isSuccessful == true) {
          // XLogger.info('拉起小程序: ${resp.errorCode} ${resp.errorMsg}, isSuccessful: ${resp.isSuccessful}');
          // XLogger.info('拉起小程序 exMsg: ${resp.extMsg}');
          // if (resp.isSuccessful) {
          //   // 支付成功
            ToastUtils.showSimpleDialog(context,
                title: '提示', content: '套餐购买成功', confirmTitle: '好的', confirmCallback: () {});
            // 刷新套餐信息
            _fetchDeviceInfo();
          // } else {
          //   // 支付失败
          //   ToastUtils.showToast('支付失败');
          // }
        }else {
          ToastUtils.showToast('支付失败');
        }
      });
    });
    alipayUtils.listenPay((resp) {
      XLogger.info('VIP支付结果：${resp.resultStatus}');
      if (resp.isSuccessful) {
        // 支付成功
        // ToastUtils.showSimpleDialog(context,
        //     title: '提示', content: '套餐购买成功', confirmTitle: '好的', confirmCallback: () {});
        ToastUtils.showToast('购买成功');
        // 刷新套餐信息
        _fetchDeviceInfo();
      } else {
        // 支付失败
        ToastUtils.showToast('支付失败');
      }
    });
    super.initState();
  }

  Future<void> _fetchDeviceInfo() async {
    await ref.read(deviceInfoProvider.notifier).refresh(() {});


    // final PaymentSuccessCallback? onPaymentSuccess;

  }

  @override
  void dispose() {
    super.dispose();
    alipayUtils.dispose();
    wxpayUtils.dispose();
  }
}
